package com.qiaoyanqing.ximalaya.discovermodule.discover_adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;

import com.qiaoyanqing.ximalaya.R;
import com.qiaoyanqing.ximalaya.discovermodule.discover_activity.DiscoverClassifySecondBaseActivity;
import com.qiaoyanqing.ximalaya.discovermodule.discover_entity.Discover_ClassifyHead;

import java.util.List;

/**
 * Created by 27265 on 2016/12/7.
 */

public class DiscoverClassifyListAdapter extends BaseAdapter {
    private List<List<Discover_ClassifyHead>> datas;
    private Context context;

    public DiscoverClassifyListAdapter(List<List<Discover_ClassifyHead>> datas, Context context) {
        this.datas = datas;
        this.context = context;
    }

//    @Override
//    public int getItemViewType(int position) {
//        return super.getItemViewType(position);
//    }



    @Override
    public int getCount() {
        return datas.size();
    }

    @Override
    public Object getItem(int i) {
        return datas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }




    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Log.d("TAG", datas.get(i).size() + "==============-----------------================");
        ViewHolder holder;
        if (view == null) {
            holder = new ViewHolder();
            if (datas.get(i).size() == 6) {
                view = LayoutInflater.from(context).inflate(R.layout.discover_classify_item1, viewGroup, false);
            } else {
                view = LayoutInflater.from(context).inflate(R.layout.discover_classify_item2, viewGroup, false);
            }

            holder.gridView = (GridView) view.findViewById(R.id.lv_gv_view);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }

        DiscoverClassifyGrideAdapter adapter = new DiscoverClassifyGrideAdapter(datas.get(i), context);
        holder.gridView.setAdapter(adapter);


        holder.gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
//                Toast.makeText(context,datas.get(i).get(position).getId(),Toast.LENGTH_SHORT).show();

                String title = datas.get(i).get(position).getTitle();
                String id = datas.get(i).get(position).getId();

                Intent intent = new Intent(context, DiscoverClassifySecondBaseActivity.class);
                intent.putExtra("title",title);
                intent.putExtra("id",id);
                context.startActivity(intent);

            }
        });

        return view;
    }

    public static class ViewHolder {
        GridView gridView;

        public GridView getGridView() {
            return gridView;
        }

        public void setGridView(GridView gridView) {
            this.gridView = gridView;
        }
    }

}
